var prefix = jsctx + "/acctTenantOperator";

var acctWithdrawApply = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;
    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageAcctWithdrawApply",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            tenantOperatorCode: $('#tenantOperatorCode').val(),
                            outTradeNo: $('#outTradeNo').val(),
                            elecStationId: $('#elecStationId').val(),
                            startTime: $('#startTime').val(),
                            endTime: $('#endTime').val()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'operatorName', title: '分销商名称', align: 'left', width: '100'},
                        {field: 'loginName', title: '分销商账号', width: '100'},
                        {field: 'outTradeNo', title: '交易订单号', width: '100'},
                        {field: 'withdrawAmount', title: '提现金额', width: '80'},
                        {field: 'withdrawElecAmount', title: '提现电费金额', width: '110'},
                        {field: 'withdrawServiceAmount', title: '提现服务费金额', width: '130'},
                        {field: 'withdrawServiceCharge', title: '提现手续费', width: '120', align: 'right',},
                        {
                            field:'',title:'收款账户',align:'left',width:'100',
                            formatter:function (value,row,index) {
                                if (row.acctBankAccount!='' && row.acctBankAccount!=null){
                                    return row.acctBankAccount;
                                } else if (row.alipayUserId!=''){
                                    return row.alipayUserId;
                                }
                            }
                        },
                        {
                            field:'',title:'收款账户名称',align:'left',width:'100',
                            formatter:function (value,row,index) {
                                if (row.accountName!='' && row.accountName!=null){
                                    return row.accountName;
                                } else if (row.alipayUserName!=''){
                                    return row.alipayUserName;
                                }
                            }
                        },
                        {
                            field: '', title: '对应账单', width: '300', align: 'left',
                            formatter: function (value, row, index) {
                                var acctBillOrderList = row.acctBillOrderList;
                                var acctBillOrder = '';
                                for (var i = 0; i < row.acctBillOrderList.length; i++) {
                                    acctBillOrder +='账单'+ (i + 1) + ':' + '电费'+ acctBillOrderList[i].elecAcctAmount + '元'+'/服务费'+acctBillOrderList[i].serviceAcctAmount+';' + acctBillOrderList[i].billStartTime + '至' + acctBillOrderList[i].billEndTime +';'+ acctBillOrderList[i].stationName + ';' + '<br>'
                                }
                                // acctBillOrderList[i].factAmount
                                return acctBillOrder;
                            }
                        },
                        {field: 'createdTime', title: '提交时间', width: '100'},
                        {field: 'remark', title: '备注', width: '100'},
                        {
                            field: '', title: '提现状态', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.status == '0') {
                                    return '<span >待审核</span>';
                                } else if (row.status == '1') {
                                    return '<span >已处理</span>';
                                } else if (row.status == '2') {
                                    return '<span >驳回</span>';
                                } else if (row.status == '3') {
                                    return '<span >提现中</span>';
                                } else if (row.status == '4') {
                                    return '<span >已到账</span>';
                                } else if (row.status == '5') {
                                    return '<span >提现失败</span>';
                                }
                            }
                        },
                        {field: '', title: '失败原因', width: '100'},
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            width: '200',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="查看发票" onclick="acctWithdrawApply.invoiceModal(\''
                                            + row.withdrawApplyId
                                            + '\')">查看发票</a> ';
                                        return a ;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function acctTenantOperatorElecStation(tenantOperatorCode) {
        location.href = prefix + "/acctTenantOperatorElecStation/" + tenantOperatorCode;
    }

    function acctBank(tenantOperatorCode) {
        location.href = prefix + "/acctBank/" + tenantOperatorCode;
    }

    function exportAcctWithdrawApply() {
        $.fileDownload(prefix + "/exportAcctWithdrawApply", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                tenantOperatorCode: $('#tenantOperatorCode').val(),
                outTradeNo: $('#outTradeNo').val(),
                elecStationId: $('#elecStationId').val(),
                startTime: $('#startTime').val(),
                endTime: $('#endTime').val()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    function invoiceModal(withdrawApplyId) {
        $.ajax({
            url: prefix + "/listAcctWithdrawInvoice",
            type: "GET",
            data: {
                'withdrawApplyId': withdrawApplyId,
            },
            success: function (data) {
                $("#invoiceModal ul").html('');
                var str = '';
                for (var i = 0; i < data.length; i++) {
                    str += "<li href='" + data[i].invoiceImg + "'>" +
                        "<a href='" + data[i].invoiceImg + "' class='fileLink' target='myFrameName'>" +
                        "<img src='../../statics/img/newEdition/pdf_1.png'>" +
                        "<div>附件" + (i + 1) + "</div>" +
                        "</a>" +
                        "</li>"
                }
                if (data[0].invoiceImg.indexOf("pdf") > 0) {
                    $("#myFrameId").attr("src", data[0].invoiceImg);
                    $(".imgWrap").addClass("hide");
                    $("#myFrameId").removeClass("hide");
                } else if (data[0].invoiceImg.lastIndexOf("/")==6){
                    $(".imgWrap img").addClass("hide");
                    $("#myFrameId").attr("src","");
                } else {
                    $(".imgWrap").removeClass("hide");
                    $(".imgWrap img").attr("src", data[0].invoiceImg).removeClass("hide");
                    $("#myFrameId").addClass("hide");
                }
                $("#invoiceModal ul").append(str);
            },
        });
        setTimeout(function () {
            layer.open({
                type: 1,
                title: '发票预览（附件1）',
                closeBtn: false,
                shade: 0.4,
                shift: 7,
                maxWidth: '622px',
                shadeClose: true,
                content: $("#invoiceModal"),
                btn: ['取消'],
                btn1: function (index) {//layer.msg('yes');    //取消
                    layer.close(index)
                },

            });
        }, 100)


    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        acctTenantOperatorElecStation: acctTenantOperatorElecStation,
        acctBank: acctBank,
        exportAcctWithdrawApply:exportAcctWithdrawApply,
        invoiceModal:invoiceModal
    };
})();
